package java学习.第12章_集合框架;

import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class C5_LinkedHashMap {
    @Test
    public void LinkedHashMap重点() {
        LinkedHashMap map = new LinkedHashMap();//LinkedHashMap中的内部类: Entry数组
        /*
        重写了new Node数组 把node数组改了 弄成了双线链表
            static class Entry<K,V> extends HashMap.Node<K,V> {
                Entry<K,V> before, after;
                Entry(int hash, K key, V value, Node<K,V> next) {
                    super(hash, key, value, next);
                }
            }
         */
        map.put(541, "wdaws");
        map.put(7864, "wdgfhaws");
        map.put(5434531, "wdartryws");
        map.put(52.31341, "gdfg");
        System.out.println(map);//可以i根据添加的顺序  遍历
        //{541=wdaws, 7864=wdgfhaws, 5434531=wdartryws, 52.31341=gdfg}
    }
    @Test
    public void LinkedHashMap的顺序问题() {
        // 默认是替换  ，  true 是将原来的删掉，添加一条新的
        LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>(16,0.75f,true);

        LinkedHashMap的key冲突顺序问题1();
        System.out.println("--------------------------------------");
        LinkedHashMap的key冲突顺序问题2();

    }
    private static void LinkedHashMap的key冲突顺序问题1() {
        LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>();
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        map.put(2,22);
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for(Map.Entry<Integer,Integer> e:entries){
            System.out.println(e.getKey() + "    " +e.getValue());
        }
    }
    private static void LinkedHashMap的key冲突顺序问题2() {
        //     链表数量            负载因子          当加入重复元素时位置改变
        //如 true: put(1,1)  put(2,1)  put(3,1)    put(2,22)    sout(1=1,3=1,2=22)
        // false : put(1,1)  put(2,1)  put(3,1)    put(2,22)    sout(1=1,2=22,3=1)
        LinkedHashMap<Integer,Integer> map = new LinkedHashMap<>(16,0.75f,true);
        map.put(1,1);
        map.put(2,2);
        map.put(3,3);
        map.put(2,22);
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for(Map.Entry<Integer,Integer> e:entries){
            System.out.println(e.getKey() + "    " +e.getValue());
        }
    }
    @Test
    public void LRU控制LinkedHashMap的最大存储数量() {
        LRU<Integer,Integer> map = new LRU<>(16);
        for (int i = 0; i < 18; i++) {
            map.put(i,i);
        }
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for(Map.Entry<Integer,Integer> e:entries){
            System.out.println(e.getKey() + "    " +e.getValue());
        }
        System.out.println(map.size());

    }
}

//最近最少使用  取巧做法
class LRU<K,V> extends LinkedHashMap<K,V>{
    private int initialCapacity;
    public LRU(int initialCapacity) {
        super();
        this.initialCapacity=initialCapacity;
    }
    @Override //如果为true  这表明需要将最老的数据给删除掉
    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return super.size()>initialCapacity;
    }

}